﻿let ev = require("./getConfig").ev;
let tcpturn = require("./tcpproxy").createtcpProxy;
let udpProxy = require("./udpproxy.js").udpProxy;
let config1 = require(ev.path);
let minport = ev.minport;
let maxport = ev.maxport;
let portList = ev.portList;
let tcpen = ev.tcpen ? true : false;
let udpen = ev.udpen ? true : false;
if (!(tcpen || udpen)) {
    console.log("必须开启其中之一tcpen|udpen| ");
    process.exit(0);
}
let offsetport = ev.offsetport || 0;
if ((minport == undefined || maxport == undefined) && portList == undefined) {
    console.log("定义命令行参数{minport，maxport}||portList为Array");
    process.exit(0);
};
console.log("tcpen:" + tcpen + ":udpen" + udpen);
function getPortList() {
    let res;
    if (portList && portList.constructor == Array) {
        res = portList;
        return res;
    }
    res = new Array(maxport - minport + 1);
    for (let i = minport, j = 0; i <= maxport; i++ , j++) {
        res[j] = i;
    }

    return res;
}
portList = getPortList();
//
let j = 0;
let itv = setInterval(function () {
    if (j >= portList.length)
    {
        clearInterval(itv);
        return;
    }
    let temport = portList[j];
    if (tcpen)
        tcpturn(temport, config1.tcplistenIp, temport + offsetport, config1.tcpturnIp);
    if (udpen)
        udpProxy(temport, config1.tcplistenIp, temport + offsetport, config1.tcpturnIp);
    j++;
}, 50);
process.on('uncaughtException', function () {
});


